
#include "bsp.h"


extern uint8_t rssi_count;
extern char ShowChar[20];
static const char *TAG = "vTask_Menu";

const T_UI_MENUITEM g_uiMenuItemsSrc[MENU_ITEM_NUM] =
{
	{UISTR_MENU,							(T_UI_MENUITEM_TYPE)(u8)0},	// 1
	{UISTR_MENU_ONE,                        UIMENU_CUSTOM},//
	{UISTR_MENU_TWO,						UIMENU_CUSTOM},//


	///{UISTR_MENU_TWO,						(T_UI_MENUITEM_TYPE)(u8)0},
	///{UISTR_MENU_TWO_ONE,					UIMENU_CUSTOM},

	{-1,									(T_UI_MENUITEM_TYPE)(u8)0},

};


T_UI_MENUITEM g_uiMenuItems[sizeof(g_uiMenuItemsSrc) / sizeof(g_uiMenuItemsSrc[0])];


void uiProcMenuBuild(void)
{
	int i, j=0;
	//一种菜单过滤
	for(i=0; i<sizeof(g_uiMenuItemsSrc) / sizeof(g_uiMenuItemsSrc[0]); i++)
	{
		g_uiMenuItems[j] = g_uiMenuItemsSrc[i];
		j++;
	}
}


static void uiProcMenuCustom_SelectMode(void)
{
	u8 nKey;
	OLED_StartDraw();
	OLED_ClrScr(0);

	Lcd_HZ_1212_CD(UISTR_HZ_SZK, 0, 2, 2);
	Lcd_HZ_1212_CD(UISTR_HZ_TCJL, 0, 2+2, 4);


	/*
	Lcd_HZ_1212_CD(UISTR_HZ_RYJCC+1, 1, 0 ,2);
	Lcd_HZ_1212_CD(UISTR_HZ_BBXX, 1, 0+2 ,2);
	Lcd_String_Small(HARDWARE_VERSION, 2+12+2+4, 127-4-(strlen(HARDWARE_VERSION)*6), 1);


	Lcd_HZ_1212_CD(UISTR_HZ_RYJCC+3, 2, 0 ,2);
	Lcd_HZ_1212_CD(UISTR_HZ_GDJGSPY+4, 2, 0+2 ,1);
	Lcd_HZ_1212_CD(UISTR_HZ_GDJGSPY+2, 2, 0+3 ,1);
	Lcd_String_Small(DATA_OF_MANUFACTURE, 2+24+4+4, 127-4-(strlen(DATA_OF_MANUFACTURE)*6), 1);
	*/

	Lcd_String_Small("ESC", 55, 12*8+8,0);
	OLED_EndDraw();

	while(1)
	{
		if( (nKey = uiKeyGetKey()) == UIKEY_NONE)
		{
			//continue;
		}
		if(nKey == UIKEY_ESC)
		{
			break;
		}
		vTaskDelay(250/portTICK_PERIOD_MS);
		///uiKeySetKeyNone();//按键清零
	}
}

static void uiProcMenuCustom_ShowInfo(void)
{
	u8 nKey;
	OLED_StartDraw();
	OLED_ClrScr(0);

	Lcd_String_Small("ESC", 55, 12*8+8,0);
	OLED_EndDraw();

	while(1)
	{
		if( (nKey = uiKeyGetKey()) == UIKEY_NONE)
		{
			//continue;
		}
		if(nKey == UIKEY_ESC)
		{
			break;
		}
		vTaskDelay(250/portTICK_PERIOD_MS);
		///uiKeySetKeyNone();//按键清零
	}
}




void uiProcMenuCustom(int nPopupMenuTitle, T_UI_MENUITEM *pUiMenuItem, int row)
{
	switch(pUiMenuItem->nMenuStr)
	{
		case UISTR_MENU_ONE://设置探测距离
			uiProcMenuCustom_SelectMode();
			break;
		case UISTR_MENU_TWO://参数查看界面
			uiProcMenuCustom_ShowInfo();
			break;

		default:
			break;
	}
}

//////////////////////////////////////////////
//处理有数值显示的
void uiProcMenuHasValue(int nPopupMenuTitle, T_UI_MENUITEM *pUiMenuItem, int row,int nCurrentIndex)
{
	u32  nValue;
	u8 bRet = FALSE;
	u8 bRet1 = FALSE;//用于小车设置是否使能


	switch(pUiMenuItem->nMenuStr)
	{

		case UISTR_MENU_TWO_ONE://探测距离
			//b0isNo==0 代表格式是1234
			///nValue = uiProcBoxNumber(&bRet,row,105,AppFlash.Park_Row,2,7,2,2,0);
			if (bRet)
			{
				

			}
			break;

		default:
			//uiLcdFatalError("HAS_VALUE!");
			break;
	}
    /*
	if(Data_Is_Modify == 1)
	{
		SaveSystemInfo();
		Data_Is_Modify = 0;
		printf("xxxData_Is_Modify1 == %d\r\n",Data_Is_Modify);
	}
    */

}


/////////////////////////////////////////////////////////////////////////
//画有值要显示的部分
void uiProcMenuDrawValue(T_UI_MENUITEM *pUiMenuItem, int row, u8 bIsReverse)
{
	u8 bIsMulti = FALSE;
	u32 dwValue = 0;
	switch(pUiMenuItem->nMenuStr)
	{
		//载重设置界面的数值显示部分
		case UISTR_MENU_ONE_THR://额定载重

		case UISTR_MENU_ONE_FOU://预警比例

			break;
		case UISTR_MENU_ONE_FIV://报警比例

			break;
		case UISTR_MENU_ONE_SIX://是否开启
			bIsMulti = TRUE;

			break;
		default:
			//uiLcdFatalError("DRAW_VALUE!");
			break;
	}

	
}


////绘画菜单界面
void uiProcMenuDraw(T_UI_MENUITEM *pUiStartMenuItem, u32 nCount,u32 nTopIndex, u32 nCurrentIndex)
{
	u32 i, nIndex;
	u8 bSelected;

	//清除下界面
	OLED_StartDraw();
	OLED_ClrScr(0);

	//显示菜单主题
	switch((pUiStartMenuItem-1)->nMenuStr)//找type对应的值
	{
		case UISTR_MENU://主菜单 
			Lcd_HZ_1212(UISTR_HZ_ZCD,0,3,3);
			break;
		case UISTR_MENU_ONE://
			//Lcd_HZ_1212(UISTR_HZ_ZCD,0,3,3);
			break;
		case UISTR_MENU_TWO://层列设置
			//Lcd_HZ_1212(UISTR_HZ_MSCL+2, 0, 3, 2);
			//Lcd_HZ_1212(UISTR_HZ_SZK, 0, 3+2, 2);
			break;
		
		default:

			break;
	}

	for(i=0; i<MENU_OF_ITEMS; i++)//循环显示一个主题下的三个项目
	{
		nIndex = nTopIndex + i;
		if (nIndex >= nCount)
			break;
		if (nIndex == nCurrentIndex)//区别显示当前选择的项目
		{
			bSelected = FALSE;
			if((nCurrentIndex+1) < 10)
			{
				Lcd_Decimal_Small(nCurrentIndex+1, (1+i)*12+4+(i+1)*2, 4, 0, 1);
				Lcd_String_Small(">", (1+i)*12+4+(i+1)*2, 10,0);
			}
			else
			{
				Lcd_String_Small(">>", (1+i)*12+4+(i+1)*2, 4, 0);
			}
		}
		else
		{
			bSelected = TRUE;
		}

		//在这里添加菜单目录的绘画
		switch(pUiStartMenuItem[nIndex].nMenuStr)//找type对应的值
		{
			//主菜单下的显示
			case UISTR_MENU_ONE://设置探测距离
				Lcd_HZ_1212_CD(UISTR_HZ_SZK, 1+i, 1, 2);
				Lcd_HZ_1212_CD(UISTR_HZ_TCJL, 1+i, 1+2, 4);
				break;
			case UISTR_MENU_TWO://参数查看界面
				Lcd_HZ_1212_CD(UISTR_HZ_CSCK, 1+i, 1, 4);
				Lcd_HZ_1212_CD(UISTR_HZ_JJSJM+3, 1+i, 1+4, 2);
				break;
			





			default:
				//i = (pUiStartMenuItem-1)->nMenuStr;
				break;
		}

		if (pUiStartMenuItem[nIndex].nType == UIMENU_HASVALUE ||
			pUiStartMenuItem[nIndex].nType == UIMENU_CUSTOM_HASVALUE)
		{
			//uiProcMenuDrawValue(&pUiStartMenuItem[nIndex], (1+i)*12+4+(i+1)*2, bSelected);
			uiProcMenuDrawValue(&pUiStartMenuItem[nIndex], (1+i)*12+4+(i+1)*2, TRUE);
		}
	}
	OLED_EndDraw();
}

void uiProcMenuSettingStart(u32 nPopupMenuTitle)
{

}



//这里主要用来最终判断每次退出一个菜单
//后是否有需要数据保存
void uiProcMenuSettingEnd(u32 nPopupMenuTitle)
{


}




//绘制某个菜单，参数是菜单位置
void uiProcMenu(u8 nPopupMenuTitle)
{
	u32 nCount, nTopIndex = 0, nCurrentIndex = 0;
	T_UI_MENUITEM *pUiStartMenuItem = &g_uiMenuItems[0], *pUiEndMenuItem;

	u8 nKey;

	uiProcMenuSettingStart(nPopupMenuTitle);

	//从第一个位置开始查找nPopupMenuTitle的位置
	while (pUiStartMenuItem->nMenuStr != nPopupMenuTitle ||
		(s32)pUiStartMenuItem->nType < 0)//这里可以解释为什么类型都是负值了
	{
		pUiStartMenuItem++;
	}
	pUiStartMenuItem++;//找到nPopupMenuTitle位置后，跳出来加1就是实际位置

	pUiEndMenuItem = pUiStartMenuItem;//结束位置从开始位置接着定位
	while ((s32)pUiEndMenuItem->nType < 0)//循环到下个菜单位置就是结束位置
	{
		pUiEndMenuItem++;
	}

	//计算这个menu下有多少个子菜单
	nCount = ((u32)pUiEndMenuItem - (u32)pUiStartMenuItem)/sizeof(T_UI_MENUITEM);

	//绘画菜单界面
	uiProcMenuDraw(pUiStartMenuItem, nCount, nTopIndex, nCurrentIndex);
	//LCD_Clear_Gram();
	//LCD_Refresh_Gram();
	uiKeySetKeyNone();
	while(1)
	{
		vTaskDelay(100/portTICK_PERIOD_MS);
		if( (nKey = uiKeyGetKey()) == UIKEY_ESC)
		{
			uiProcMenuSettingEnd(nPopupMenuTitle);
			break;
		}
		switch(nKey)
		{
			case UIKEY_OK:
				if (pUiStartMenuItem[nCurrentIndex].nType == UIMENU_POPUP)//处理有下拉菜单的选项
				{

					uiProcMenu(pUiStartMenuItem[nCurrentIndex].nMenuStr);

				}
				else if(pUiStartMenuItem[nCurrentIndex].nType == UIMENU_HASVALUE)//有值改变的情况处理
				{

					//uiProcMenuHasValue(nPopupMenuTitle, &pUiStartMenuItem[nCurrentIndex],
						//1 + (nCurrentIndex - nTopIndex),nCurrentIndex);
					uiProcMenuHasValue(nPopupMenuTitle, &pUiStartMenuItem[nCurrentIndex],
						(1+(nCurrentIndex-nTopIndex))*12+4+(1+(nCurrentIndex-nTopIndex))*2,nCurrentIndex);
				}
				else if (pUiStartMenuItem[nCurrentIndex].nType == UIMENU_CUSTOM)//单独显示界面
				{
					uiProcMenuCustom(nPopupMenuTitle, &pUiStartMenuItem[nCurrentIndex],1 + (nCurrentIndex - nTopIndex));
				}

				uiProcMenuDraw(pUiStartMenuItem, nCount, nTopIndex, nCurrentIndex);

				break;
			case UIKEY_UP:
				if (nCurrentIndex > 0)
					nCurrentIndex --;

				if (nCurrentIndex < nTopIndex)
					nTopIndex = nCurrentIndex;

				uiProcMenuDraw(pUiStartMenuItem, nCount, nTopIndex, nCurrentIndex);
				break;
			case UIKEY_DOWN:
				if (nCurrentIndex < nCount - 1)
					nCurrentIndex ++;
				///if (nCurrentIndex > nTopIndex + 2)
					///nTopIndex = nCurrentIndex - 2;
				if (nCurrentIndex > nTopIndex +  (MENU_OF_ITEMS-1))//这个值的设定和屏幕显示菜单数有关
					nTopIndex = nCurrentIndex -  (MENU_OF_ITEMS-1);

				uiProcMenuDraw(pUiStartMenuItem, nCount, nTopIndex, nCurrentIndex);
				break;
			default:
				break;
		}

        uiKeySetKeyNone();
		///if(CheckAndShowError() == 1)//在操作界面时刻检测错误状态并显示
		///{
			///break;
		///}
	}
}


u32 change_mode=1;//组号==0  编号==1

void Main_Show(u8 ret)
{

	OLED_StartDraw();
	GUI_SetColor(1,0);

	OLED_ClrScr(0x0);

	///extern char ShowChar[20];

	Lcd_HZ_1212(UISTR_HZ_JJSJM,0,1,3);//接近式
	Lcd_HZ_1212(UISTR_HZ_ZWQQ,0,4,2);//组网
	Lcd_HZ_1212(UISTR_HZ_JJSJM+3,0,6,2);//界面

	Lcd_String_Small(&ShowChar[0],16,10,0);//显示上传mac地址

	Lcd_HZ_1212(UISTR_HZ_ZWQQ,2,0,4);//组网请求
	Lcd_HZ_1212(UISTR_HZ_CSH,2,4,2);//次数
	Lcd_Decimal_Small(rssi_count,30,80,0,2);//显示次数数字


	if(rssi_count >= 3)//显示是否可以注册
	{
		Lcd_HZ_1212(UISTR_HZ_KYBH,2,8,2);//可以
		Lcd_HZ_1212(UISTR_HZ_ZCCGS,3,8,2);//注册
	}
	else
	{
		Lcd_HZ_1212(UISTR_HZ_JJSJM+5,2,8,2);//00
		Lcd_HZ_1212(UISTR_HZ_JJSJM+5,3,8,2);//00
	}


	if(ret == 1)//显示是否注册成功
	{
		Lcd_HZ_1212(UISTR_HZ_ZCCGS,3,3,4);//注册成功
	}



	Lcd_HZ_1212(UISTR_HZ_ZWQQ,4,0,1);//组
	Lcd_HZ_1212(UISTR_HZ_KYBH+3,4,1,1);//号
	Lcd_Decimal_Small(App.Tosop_Edu_Channel,54,30,0,2);//显示组号

	Lcd_HZ_1212(UISTR_HZ_KYBH+2,4,5,2);//编号
	Lcd_Decimal_Small(App.Tosop_Edu_column,54,90,0,2);//显示编号 

	if(change_mode%2 == 0)//绘制切换底线
	{
		OLED_DrawLine(0,63,48,63,1);
		OLED_DrawLine(60,63,105,63,0);
	}
	else
	{
		OLED_DrawLine(0,63,48,63,0);
		OLED_DrawLine(60,63,105,63,1);
	}
	
	OLED_EndDraw();

}



void vTask_Menu( void *pvParameters )
{

    u8 nKey=0;
    ESP_LOGI(TAG, "vTask_Menu is in !!!");

	uint8_t Cru_rssi_count=0;
	uint8_t Cru_Tosop_Edu_Channel=0;
	uint8_t Cru_Tosop_Edu_column=0;
	u32 Cru_change_mode=0;
	

    uiProcMenuBuild();

	Main_Show(0);


    while(1)
    {
        
		//触发界面改变
		if((Cru_rssi_count != rssi_count)||(Cru_Tosop_Edu_Channel != App.Tosop_Edu_Channel)||(Cru_Tosop_Edu_column != App.Tosop_Edu_column)||(Cru_change_mode != change_mode))//
		{
			Main_Show(0);
			Cru_rssi_count = rssi_count;
			Cru_Tosop_Edu_Channel = App.Tosop_Edu_Channel;
			Cru_Tosop_Edu_column = App.Tosop_Edu_column;
			Cru_change_mode = change_mode;
		}
		else{}

        nKey = uiKeyGetKey();//获取按键值
        if( nKey== UIKEY_ESC)
		{
			change_mode++;
		}
        else if(nKey== UIKEY_OK)
        {
            uiProcMenu(UISTR_MENU);

			rssi_count=0;
			///OLED_StartDraw();
			Main_Show(0);
			///OLED_EndDraw();
        }
        else if(nKey== UIKEY_UP)
        {
			if(change_mode%2 == 0)//组号
			{
				App.Tosop_Edu_Channel++;
			}
			else
			{
				App.Tosop_Edu_column++;
			}

			if(App.Tosop_Edu_Channel>=200)
			{
				App.Tosop_Edu_Channel=200;
			}
			if(App.Tosop_Edu_column>=200)
			{
				App.Tosop_Edu_column=200;
			}



        }
        else if(nKey== UIKEY_DOWN)
        {
			if(change_mode%2 == 0)//组号
			{
				App.Tosop_Edu_Channel--;
			}
			else
			{
				App.Tosop_Edu_column--;
			}

			if(App.Tosop_Edu_Channel<=1)
			{
				App.Tosop_Edu_Channel=1;
			}
			if(App.Tosop_Edu_column<=1)
			{
				App.Tosop_Edu_column=1;
			}



        }else if(nKey == UIKEY_ESC_LONG)
		{

			if(Device_SetInfo() == 1)//注册成功
			{
				Main_Show(1);
			}
			vTaskDelay((1500)/portTICK_RATE_MS);//
			Main_Show(0);

			uiKeySetKeyNone();//按键清零
		}
		
		
		else{}

        uiKeySetKeyNone();//按键清零

        vTaskDelay((100)/portTICK_RATE_MS);//
       
    }
    vTaskDelete(NULL);
} 